// config

var app = angular.module('app')
    .config(
        ['$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
            function ($controllerProvider, $compileProvider, $filterProvider, $provide) {

                // lazy controller, directive and service
                app.controller = $controllerProvider.register;
                app.directive = $compileProvider.directive;
                app.filter = $filterProvider.register;
                app.factory = $provide.factory;
                app.service = $provide.service;
                app.constant = $provide.constant;
                app.value = $provide.value;
                // string-format init format method
                format.extend(String.prototype, {});
            }
        ])
    .config(['$translateProvider', function ($translateProvider) {
        $translateProvider.useSanitizeValueStrategy('sanitize');
        // Register a loader for the static files
        // So, the module will search missing translation tables under the specified urls.
        // Those urls are [prefix][langKey][suffix].
        $translateProvider.useStaticFilesLoader({
            prefix: 'l10n/',
            suffix: '.json'
        });
        // Tell the module what language to use by default
        $translateProvider.preferredLanguage('zh');
        // Tell the module to store the language in the local storage
        $translateProvider.useLocalStorage();
    }])
    .config(['$httpProvider', function ($httpProvider) {
        // $httpProvider.defaults.withCredentials = true;
        // http interceptor
        $httpProvider.interceptors.push([
            '$injector',
            function ($injector) {
                return $injector.get('authInterceptor');
            }
        ]);
    }])
    // textAngular
    .config(['$provide', function ($provide) {
        $provide.decorator('taOptions', ['taRegisterTool', '$modal', '$delegate',
            function (taRegisterTool, $modal, taOptions, $delegate) {
                // $delegate is the taOptions we are decorating
                // here we override the default toolbars specified in taOptions.
                taOptions.toolbar = [
                    ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'pre', 'quote'],
                    ['bold', 'italics', 'underline', 'strikeThrough', 'ul', 'ol', 'redo', 'undo', 'clear'],
                    ['justifyLeft', 'justifyCenter', 'justifyRight', 'indent', 'outdent'],
                    ['html', 'insertImage', 'insertLink', 'insertVideo'],
                    ['charcount']
                ];

                // console.log(JSON.stringify(taOptions.toolbar));
                // Create our own insertImage button
                taRegisterTool('customInsertImage', {
                    display: '<button type="button" class="btn btn-default" name="customInsertImage" ng-click="action()" ' +
                    'title="添加本地图片"><i class="fa fa-file-picture-o"></i></button>',
                    // iconclass: "fa fa-picture-o",
                    action: function (deferred, restoreSelection) {
                        var textAngular = this;
                        var savedSelection = rangy.saveSelection();
                        var $modalInstance = $modal.open({
                            // Put a link to your template here or whatever
                            templateUrl: 'tpl/fileUploadModal/fileUploadModal.html',
                            size: 'lg',
                            controller: 'fileUploadModalCtrl'
                        });

                        $modalInstance.result.then(function (result) {
                            // restoreSelection();
                            // textAngular.$editor().wrapSelection('insertImage', qiniu.domain + '/' + result);
                            // document.execCommand('insertImage', true, result);
                            rangy.restoreSelection(savedSelection);
                            textAngular.$editor().wrapSelection('insertImage', result);
                            deferred.resolve();
                        });

                        return false;
                    }
                });

                // Now add the button to the default toolbar definition
                // Note: It'll be the last button
                taOptions.toolbar[3].push('customInsertImage');
                return taOptions;
            }]);
    }])
    // ng-clip
    .config(['ngClipProvider', function (ngClipProvider) {
        ngClipProvider.setPath("http://cdn.bootcss.com/zeroclipboard/2.2.0/ZeroClipboard.swf");
    }]);
